﻿using Identity_JWT.Dao.Model;
using Microsoft.AspNetCore.Identity;

namespace Identity_JWT.IdentityPolicy
{
    /// <summary>
    /// 自定义Identity密码验证策略
    /// </summary>
    public class CustomPasswordPolicy: PasswordValidator<User>
    {

        public override async Task<IdentityResult> ValidateAsync(UserManager<User> manager, User user, string password)
        {
           var result= await base.ValidateAsync(manager, user, password);
            List<IdentityError> errors = result.Succeeded ? new List<IdentityError>() : result.Errors.ToList();

            if (password.ToLower().Contains(user.UserName.ToLower()))
            {
                errors.Add(new IdentityError
                {
                    Description = "密码不能包含用户名"
                });
            }

            if (password.Contains("123"))
            {
                errors.Add(new IdentityError
                {
                    Description = "密码不能包含123数字序列"
                });
            }
            return errors.Count == 0 ? IdentityResult.Success : IdentityResult.Failed(errors.ToArray());
        }
    }
}
